<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit|ie-comp|ie-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <title>公司列表</title>
  <script src="../../../static/kaige/js/szload.js"></script>
  <style>
    .kaige-body-form .layui-form-item .layui-form-label {
      width: 120px;
    }

    .logoimg {
      width: 60px;
      height: 60px;
    }
  </style>
</head>

<body>
  <div class="kaige-body layui-form kaige-btn-radius" lay-filter="company-index">
    <div class="layui-btn-group" style="margin-top:5px;margin-bottom: 5px;">
      <button type="button" class="layui-btn layui-bg-blue layui-btn-sm option-add">
        <i class="layui-icon layui-icon-add-circle"></i> 添加
      </button>
      <button type="button" class="layui-btn layui-bg-blue layui-btn-sm reload">
        <i class="fa fa-refresh"></i> 刷新
      </button>
      <button type="button" class="layui-btn layui-bg-blue layui-btn-sm open-all">
        <i class="fa fa-angle-double-down"></i> 全展开
      </button>
      <button type="button" class="layui-btn layui-bg-blue layui-btn-sm close-all">
        <i class="fa fa-angle-double-right"></i> 全关闭
      </button>
    </div>
    <table class="layui-table" id="companyTable" lay-filter="companyTable" lay-size="sm"></table>
  </div>

  <script>
    load_init_js(function () {
      layui.config({
        base: szoapcpath + '/static/kaige/layuiextend/'
      }).use(['treeTable', 'authtree', 'kaige', 'tableSelect'], function () {
        var form = layui.form, layer = layui.layer, table = layui.table, treeTable = layui.treeTable, element = layui.element,
          $ = layui.$, authtree = layui.authtree, kaige = layui.kaige, laydate = layui.laydate,tableSelect = layui.tableSelect;
        var treedata;
        //查看图片
        window.viewImage = function (imgUrl) { kaige.viewimage({ maxwidth: 600, maxheight: 600, url: imgUrl }); }
        // 渲染表格
        var companyre = treeTable.render({
          elem: "#companyTable",
          url: "/hrm.company/index",
          icon_key: 'name',
          top_value: 0,
          primary_key: 'id',
          parent_key: 'pid',
          is_cache: true,
          cols: [
            { key: 'id', title: 'ID', width: '30px' },
            {
              key: 'logo', title: 'LOGO', width: '60px', template: function (d) {
                return '<img src="' + d.logo + '" onclick="viewImage(\'' + d.logo + '\')" class="logoimg" alt=""/>';
              }
            },
            { key: 'name', title: '简称', width: '120px' },
            { key: 'fullname', title: '全称', width: '220px' },
            {
              key: 'pobj', title: '上级单位', width: '160px', template: function (item) {
                if (item.pobj && item.pobj.name) {
                  return item.pobj.name;
                }
                return '无'
              }
            },
            { key: 'headman', title: '负责人', width: '100px' },
            { key: 'city', title: '所在城市', width: '120px' },
            { key: 'sort', title: '排序', width: '40px' },
            {
              key: 'status', title: '状态', width: '70px', align: 'center', template: function (item) {
                return '<input type="checkbox"  value=" ' + item.id + ' " ' + (item.status == 1 ? 'checked=""' : '') + ' lay-filter="statusdemo" lay-skin="switch" lay-text="是|否">';
              }
            },
            {
              title: '操作', align: 'center', width: '140px', template: function (item) {
                var html = '<div class="layui-btn-group">';
                html += '<button type="button" class="layui-btn layui-bg-blue layui-btn-sm option-edit" data-pid="' + item.pid + '" data-id="' + item.id + '" title="编辑"><i class="fa fa-pencil-square"></i></button>';
                html += '<button type="button" class="layui-btn layui-bg-blue layui-btn-sm option-del" data-title="' + item.name + '" data-id="' + item.id + '" title="删除"><i class="fa fa-trash-o"></i></button>';
                html += '</div>';
                return html;
              }
            },
          ],
          end: function (e) {
            treedata = e.data;
            form.render(null, 'company-index');
            //编辑
            $('.option-edit').click(function (e) {
              var id = $(this).attr('data-id');
              var pid = $(this).attr('data-pid');
              kaige.form({
                title: '<i class="layui-icon layui-icon-edit"></i> 编辑'
                , url: '/hrm.company/edit' //数据源
                , data: { id: id }
                , id: 'kaige'
                , area:['800px','450px']
                , callbefore: function () {
                  treelist();
                  settopcompany();
                  duimg();
                  dselhead();
                  getlatlon_hand();
                  //找出父菜单名对入上级模块
                  if (pid < 1) {
                    pid_name = '顶级单位'
                  } else {
                    pid_name = get_pid_name_k(treedata, pid, 'name');
                  }
                  $('#company_pid_name').val(pid_name);
                }
                , postbefore: function (udata) {
                  var updata = udata;
                  updata.is_lock = updata.is_lock ? 1 : 2;
                  updata.is_auto_kbzj = updata.is_auto_kbzj ? 1 : 2;
                  updata.type = updata.type ? 1 : 2;
                  return updata;
                }
                , callback: function (res) {
                  res.status && treeTable.render(companyre);
                }
              });
            });

            //删除
            $('.option-del').click(function (e) {
              var id = $(this).attr('data-id');
              var title = $(this).attr('data-title');
              layer.confirm('真的要删除么', { icon: 3, title: '名称：' + title }, function (index) {
                $.post('/hrm.company/del', { id: id }, function (res) {
                  if (res.status) {
                    layer.msg(res.msg, { time: 1000 }, function () {
                      treeTable.render(companyre);
                      layer.close(index);
                    });
                  } else {
                    layer.msg(res.msg);
                  }
                });
              });
            });
          },
        });

        // 全部展开
        $('.open-all').click(function () {
          treeTable.openAll(companyre);
        })
        // 全部关闭
        $('.close-all').click(function () {
          treeTable.closeAll(companyre);
        });

        // 刷新
        $('.reload').click(function () {
          location.reload();
        });

        //设置顶级单位
        function settopcompany() {
          $('#settopcompany').on('click', function () {
            $('#company_pid_name').val('顶级单位');
            $('#company_pid_id').val(0);
          });
        }
        //模块树形
        function treelist() {
          treeTable.render({
            elem: '#treecompany',
            data: treedata,
            icon_key: 'name',
            top_value: 0,
            primary_key: 'id',
            parent_key: 'pid',
            cols: [
              { key: 'name', title: '名称' },
            ],
            end: function (e) { },
          });
          $('#treecompany').on('click', '[data-down]', function () {
            $('#company_pid_name').val($(this).text());
            $('#company_pid_id').val($(this).parent().attr('data-id'));
          });
          $('#companysel').click(function () {
            $(this).parent().hasClass('layui-form-selected') ? $(this).next().hide() : $(this).next().show(), $(this).parent().toggleClass('layui-form-selected');
          });
          $(document).on("click", function (i) {
            !$(i.target).parent().hasClass('layui-select-title') && !$(i.target).parents('table').length && !(!$(i.target).parents('table').length && $(i.target).hasClass('layui-icon')) && $("#company_select").removeClass("layui-form-selected").find('.layui-anim').hide();
          });
        }

        function dselhead() {
          tableSelect.render({
            elem: '#sel_headman',
            searchKey:'keywords',
            checkedKey: 'id',
            table: {
              url: '/select/employee',
              cols: [[
                { type: 'radio' },
                { field: 'id', title: 'ID',hide:true },
                { field: 'real_name', title: '姓名' },
                { field: 'phone', title: '手机' },
                { field: 'dept_id_name', title: '部门' }
              ]]
            },
            done: function (elem, data) {
              var NEWJSON = [],ids = [];
              layui.each(data.data, function (index, item) {
                NEWJSON.push(item.real_name);
                ids.push(item.id);
              })
              elem.val(NEWJSON.join(","));
              $('input[name=headid]').val(ids.join(","));
            }
          })
        }

        function duimg() {
          //上传logo
          kaige.upload_multi_files({
            input_id: '#logo_imgid',
            img_id: '#logo_imgsrc',
            btn: '#logo_upbtn',
            url: szoa.client.path + '/pages/files/upfiles.html?id=7'
          });

          kaige.upload_multi_files({
            input_id: '#inputimgguid1',
            img_id: '#srcimgurl1',
            btn: '#editimg1',
            url: szoa.client.path + '/pages/files/upfiles.html?id=7'
          });
          kaige.upload_multi_files({
            input_id: '#inputimgguid2',
            img_id: '#srcimgurl2',
            btn: '#editimg2',
            url: szoa.client.path + '/pages/files/upfiles.html?id=7'
          });
          kaige.upload_multi_files({
            input_id: '#inputimgguid3',
            img_id: '#srcimgurl3',
            btn: '#editimg3',
            url: szoa.client.path + '/pages/files/upfiles.html?id=7'
          });
        }

        //添加
        $('.option-add').click(function (e) {
          kaige.form({
            title: '<i class="layui-icon layui-icon-add-circle"></i> 添加'
            , url: '/hrm.company/add' //数据源
            , data: {}
            , id: 'kaige'
            , area:['800px','450px']
            , callbefore: function () {
              treelist();
              settopcompany();
              duimg();
              dselhead();
              getlatlon_hand();
            }
            , postbefore: function (udata) {
              var updata = udata;
              updata.is_lock = updata.is_lock ? 1 : 2;
              updata.is_auto_kbzj = updata.is_auto_kbzj ? 1 : 2;
              updata.type = updata.type ? 1 : 2;
              return updata;
            }
            , callback: function (res) {
              res.status && treeTable.render(companyre);
            }
          });
        });

        //监听启禁用操作
        form.on('switch(statusdemo)', function (obj) {
          var udata = {
            id: (this.value).trim(),
            field: 'status',
            value: obj.elem.checked ? 1 : 2
          };
          szoa.post('/hrm.company/setdisabled', udata, function (res) {
            layer.tips(udata.value == 1 ? '启用' : '禁用', obj.othis);
          });
        });

        function getlatlon_hand() {
          $("#getpoint").on('click', function (e) {
            var url = '//lbs.qq.com/tool/getpoint/getpoint.html';
            layer.open({
              title: '坐标拾取器',
              shade: 0,
              type: 2,
              content: url,
              area: ['980px', '550px'],
              maxmin: true,
            });
          });
        }


      });
    });
  </script>
  <script type="text/html" id="kaige_form_html">
    <form class="layui-form layui-form-pane kaige-body-form" lay-filter="kaige_form_lay_filter">
      {{# if(d.id){ }}
        <input type="hidden" name="id" value="{{ d.id }}" />
      {{# } }}
      <div class="layui-tab layui-tab-brief" style="margin-top:1px;">
          <ul class="layui-tab-title">
              <li class="layui-this">
                <span class="layui-badge layui-bg-blue">1</span>&nbsp;&nbsp;&nbsp;&nbsp;基本信息
              </li>
              <li>
                <span class="layui-badge layui-bg-blue">2</span>&nbsp;&nbsp;&nbsp;&nbsp;位置信息
              </li>
              <li>
                <span class="layui-badge layui-bg-blue">3</span>&nbsp;&nbsp;&nbsp;&nbsp;档案信息
              </li>
          </ul>
          <div class="layui-tab-content">
              <div class="layui-tab-item layui-show">
                  <div class="layui-form-item">
                      <label class="layui-form-label">单位上级</label>
                      <div class="layui-input-inline" style="width:220px">
                          <div class="layui-unselect layui-form-select" id="company_select">
                              <div class="layui-select-title" style="position: relative;" id="companysel">
                                  <input type="text" placeholder="选择单位" value="{{ d.name ||'' }}" readonly="" class="layui-input layui-unselect" id="company_pid_name" />
                                  <input type="hidden" name="pid" value="{{ d.pid || ''}}" id="company_pid_id"/>
                                  <i class="layui-edge"></i>
                              </div>
                              <div id="treecompany"  class="layui-anim layui-treetable-selectbox" style="width: 320px;"></div>
                          </div>
                      </div>
                      <div class="layui-input-inline border3" style="width:219px">
                          <button type="button" id="settopcompany" class="layui-btn layui-bg-blue layui-btn-sm" style="margin-top:3px;margin-left: 5px;">设置为顶级单位 </button>
                      </div>        
                  </div>
                  <div class="layui-form-item">
                      <label class="layui-form-label">单位简称</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="name" required lay-verify="required"  value="{{ d.name || '' }}" placeholder="请输入单位简称" autocomplete="off" class="layui-input"/>
                      </div>
                      <label class="layui-form-label">编码</label> 
                      <div class="layui-input-inline" style="width:219px">
                          <input type="text" name="code" required lay-verify="required"  value="{{ d.code || '' }}" placeholder="请输入编码" autocomplete="off" class="layui-input"/>
                      </div>        
                  </div>

                  <div class="layui-form-item">
                      <label class="layui-form-label">单位全称</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="fullname" required lay-verify="required"  value="{{ d.fullname || '' }}" placeholder="请输入单位全称" autocomplete="off" class="layui-input"/>
                      </div>
                      <label class="layui-form-label">负责人</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="headman" value="{{ d.headman ||'' }}" autocomplete="off" class="layui-input" id="sel_headman"/>
                          <input type="hidden" name="headid" value="{{ d.headid || ''}}"/>
                      </div>
                  </div>

                  <div class="layui-form-item">
                      <label class="layui-form-label">LOGO</label>
                      <div class='layui-input-inline' style="width: 100px;">
                          <img id="logo_imgsrc" style="height:auto;max-height:32px;min-width:32px" onclick="viewImage(this.src)" src="{{ d.logo_url || '' }}"/>
                          <input type="hidden" name="logo" id="logo_imgid" value="{{ d.logo || ''}}">
                      </div>
                      <div class='layui-input-inline' style="width: 111px;">
                          <a class="layui-btn layui-bg-blue layui-btn-sm" id="logo_upbtn">上传</a>
                      </div>
                      <label class="layui-form-label">电话</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="tel" name="tel" value="{{ d.tel||'' }}" autocomplete="off" class="layui-input"/>
                      </div>
                  </div>

                  <div class="layui-form-item">
                      
                      <label class="layui-form-label">传真</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="tel" name="fax" class="layui-input" value="{{ d.fax ||'' }}" />
                      </div> 
                  </div>

                  <div class="layui-form-item">
                      
                      <label class="layui-form-label">排序</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="sort" class="layui-input" value="{{ typeof(d.sort)==='number' ? d.sort: '' }}" />
                      </div>  
                      <label class="layui-form-label">启/禁用</label>
                      <div class="layui-input-inline border3" style="width:219px">
                          <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁用" {{#  if(d.status ==1 ){ }} checked {{#  } }} />
                      </div> 
                  </div>
              </div>

              <div class="layui-tab-item">
                  <div class="layui-form-item">
                      <label class="layui-form-label">地址</label>
                      <div class="layui-input-inline border3" style="width:219px">
                          <input type="text" name="address" class="layui-input" value="{{ d.address||'' }}"/>
                      </div>
                      <div class="layui-input-inline">
                          <button type="button" id="getpoint" class="layui-btn layui-bg-blue layui-btn-sm" style="margin-top:3px;margin-left: 5px;">坐标拾取器</button>
                      </div>
                  </div>
                  <div class="layui-form-item">
                      <label class="layui-form-label">国家</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="nation" class="layui-input" value="{{ d.nation ||'' }}" />
                      </div>
                      <label class="layui-form-label">省份</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="province" class="layui-input" value="{{ d.province || '' }}" />
                      </div>                      
                  </div>
                  <div class="layui-form-item">
                      <label class="layui-form-label">城市</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="city" class="layui-input" value="{{ d.city || '' }}" />
                      </div>                      
                      <label class="layui-form-label">县/市</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="district" class="layui-input" value="{{ d.district ||'' }}" />
                      </div>
                  </div>  
                  <div class="layui-form-item">
                      <label class="layui-form-label">街/道</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="street" class="layui-input" value="{{ d.street|| '' }}" />
                      </div>
                  </div>
                  <div class="layui-form-item">
                      <label class="layui-form-label">纬度</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="lat" class="layui-input" value="{{ d.lat|| '' }}" />
                      </div>                      
                      <label class="layui-form-label">经度</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="lon" class="layui-input" value="{{ d.lon|| '' }}" />
                      </div>                      
                  </div>

              </div>
              <div class="layui-tab-item">
                  <div class="layui-form-item">
                      <label class="layui-form-label">法人姓名</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="bl_sfz_name" class="layui-input" value="{{ d.bl_sfz_name|| '' }}" />
                      </div>
                      <label class="layui-form-label">法人手机号</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="tel" name="bl_phone" value="{{ d.bl_phone || '' }}" autocomplete="off" class="layui-input" />
                      </div>
                  </div>
                  <div class="layui-form-item">
                      <label class="layui-form-label">法人身份证</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="bl_sfz_code" class="layui-input" value="{{ d.bl_sfz_code || '' }}" />
                      </div>
                      <label class="layui-form-label">身份证图</label>
                      <div class='layui-inline'>
                          <img id="srcimgurl2" style="height:auto;max-height:32px;min-width:32px" onclick="viewImage(this.src)" src="{{ d.bl_sfz_img_url || '' }}"/>
                          <input type="hidden" name="bl_sfz_img" id="inputimgguid2" value="{{ d.bl_sfz_img || '' }}">
                      </div>
                      <div class='layui-inline'>
                          <a class="layui-btn layui-bg-blue layui-btn-sm" id="editimg2">上传图片</a>
                      </div>
                  </div>
                  
                  <div class="layui-form-item">
                      <label class="layui-form-label">营业执照代码</label>
                      <div class="layui-input-inline" style="width:220px">
                          <input type="text" name="bl_code" class="layui-input" value="{{ d.bl_code|| '' }}" />
                      </div>  
                      <label class="layui-form-label">营业执照图</label>
                      <div class='layui-inline'>
                          <img id="srcimgurl1" style="height:auto;max-height:32px;min-width:32px" onclick="viewImage(this.src)" src="{{ d.bl_img_url || '' }}"/>
                          <input type="hidden" name="bl_img" id="inputimgguid1" value="{{ d.bl_img|| '' }}">
                      </div>
                      <div class='layui-inline'>            
                          <a class="layui-btn layui-bg-blue layui-btn-sm" id="editimg1">上传图片</a>
                      </div>
                  </div>

              </div>
          </div>
      </div>
      <div class="layui-form-item layui-hide">
        <button class="layui-hide" lay-submit="" id="kaige_form_btn_save" lay-filter="kaige_form_btn_save">保存数据</button>
      </div>
  </form>
  </script>
</body>

</html>